﻿using System;
using System.Windows.Forms;
using Wangzeran.CodeMaker.GUI.Codes;
using Wangzeran.CodeMaker.GUI.Modal;
using Wangzeran.CodeMaker.GUI.Utils;
using Wangzeran.CodeMaker.Core.DB;

namespace Wangzeran.CodeMaker.GUI.Forms
{
    /// <summary>
    /// 数据库连接信息设置
    /// </summary>
    public partial class DBForm : Form
    {
        private XmlMgt opXml = new XmlMgt();

        public DBForm()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 显示相应数据库的连接面板
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void radDb_CheckedChanged(object sender, EventArgs e)
        {
            if (sender is RadioButton)
            {
                // 隐藏所有控件
                for (int i = 0; i < gbDBConn.Controls.Count; i++)
                {
                    gbDBConn.Controls[i].Visible = false;
                }

                RadioButton rad = sender as RadioButton;
                string pnl = rad.Tag == null ? null : rad.Tag.ToString();
                if(pnl == null)
                {
                    return;
                }
                // 显示对应数据库的Panel
                gbDBConn.Controls[pnl].Visible = true;
            }
        }

        /// <summary>
        /// 关闭
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// 显示数据库连接信息面板时获取数据库连接信息对象
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pnlDb_VisibleChanged(object sender, EventArgs e)
        {
            if (sender is Panel)
            {
                Panel pnl = sender as Panel;
                string type = pnl.Tag == null ? null : pnl.Tag.ToString();
                if (type == null)
                {
                    return;
                }
                string connString = opXml.GetConnString(type);
                DBModal modal = new DBModal(type, connString);
                switch(type)
                {
                    case DBModal.MSSQL:
                        {
                            InitMssqlText(modal);
                            break;
                        }
                    default:
                        {
                            break;
                        }
                }
            }
        }

        /// <summary>
        /// 初始化MSSQL数据库连接信息
        /// </summary>
        /// <param name="modal">数据库连接实体</param>
        private void InitMssqlText(DBModal modal)
        {
            txtMSSQL_DataSource.Text = modal.DataSource;
            txtMSSQL_UserName.Text = modal.User;
            txtMSSQL_Password.Text = modal.Pwd;
            radMSSQL_AuthzWindows.Checked = modal.IsWindowsAuthz;
            radMSSQL_AuthzSqlServer.Checked = !modal.IsWindowsAuthz;
        }

        /// <summary>
        /// Windows验证方式
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void radAuthzWindows_CheckedChanged(object sender, EventArgs e)
        {
            txtMSSQL_UserName.ReadOnly = true;
            txtMSSQL_Password.ReadOnly = true;
        }
        /// <summary>
        /// MS SQL Server验证方式
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void radAuthzSqlServer_CheckedChanged(object sender, EventArgs e)
        {
            txtMSSQL_UserName.ReadOnly = false;
            txtMSSQL_Password.ReadOnly = false;
        }

        /// <summary>
        /// 测试数据库连接
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnTestConn_Click(object sender, EventArgs e)
        {
            bool isSuccess = false;
            DBModal modal = GetSelectedServer();
            if (modal == null)
            {
                return;
            }
            if (radMssql.Checked)
            {
                isSuccess = DBMgt.MSSQL_TestConnection(modal.ToString());
            }

            if (isSuccess)
            {
                MessageHelper.ShowInfomation("提示", "数据库连接成功");
            }
            else
            {
                MessageHelper.ShowWarning("错误", "数据库连接失败");
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            DBModal modal = GetSelectedServer();
            opXml.SetConnString(modal.DatabaseType, modal.ToString());
            MessageHelper.ShowInfomation("提示", "数据库配置保存成功");
        }

        /// <summary>
        /// 获取当前选中的数据库
        /// </summary>
        /// <returns></returns>
        private DBModal GetSelectedServer()
        {
            DBModal modal = new DBModal();
            if (radMssql.Checked)
            {
                modal.DatabaseType = DBModal.MSSQL;
                modal.DataSource = txtMSSQL_DataSource.Text;
                modal.IsWindowsAuthz = radMSSQL_AuthzWindows.Checked;
                modal.User = txtMSSQL_UserName.Text.Trim();
                modal.Pwd = txtMSSQL_Password.Text.Trim();
            }
            return modal;
        }

    }
}
